#include <iostream>
#include <fstream>
#include <sstream>
#include <map>
#include <cstdlib>
#include "charcnt.h"

using namespace std;

void CharCnt::fromFile (const char* filename, map<char, int>& char_cnt, 
						int display) {
	string buffer;
	ifstream in(filename);
	if (!in) {
		cout<<"Unable to open file \"";
		cout<<filename<<"\", Exit"<<endl;
		exit(1);
	}

	while (getline(in, buffer)) {
		for (int i=0; i<buffer.size(); i++) {
			++char_cnt[buffer[i]];
		}
//		++char_cnt['\n'];
	}
	in.close();
	
	if (display==1) {
		typedef map<char, int> Mapci;
		Mapci::iterator map_it=char_cnt.begin();
		while(map_it !=char_cnt.end()) {
			cout<<"<'"<<map_it->first<<"', ";
			cout<<map_it->second<<">"<<endl;
			++map_it;
		}
	}

}

void CharCnt::fromCin (map<char, int>& char_cnt) {
	cout<<"Please input the message"<<endl;
	string buffer;
	getline(cin, buffer);
	
	for (int i=0; i<buffer.size(); i++) {
		++char_cnt[buffer[i]];
	}

	typedef map<char, int> Mapci;
	Mapci::iterator map_it=char_cnt.begin();
	while(map_it !=char_cnt.end()) {
		cout<<"<'"<<map_it->first<<"', ";
		cout<<map_it->second<<">"<<endl;
		++map_it;
	}
	
}
